package com.chdwmp.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.chdwmp.product.bean.app.AgentUserReationVo;
import com.chdwmp.product.bean.pay.AliPrePayReqVo;
import com.chdwmp.product.bean.pay.OrderInfoBo;
import com.chdwmp.product.constant.PayConstants;
import com.chdwmp.product.constant.Status;
import com.chdwmp.product.controller.PayController;
import com.chdwmp.product.exception.CommonException;
import com.chdwmp.product.mapper.TRegisterPayMapper;
import com.chdwmp.product.service.PayService;
import com.chdwmp.product.utils.DateUtils;
import com.chdwmp.product.utils.SequenceUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class PayServiceImpl implements PayService {


    @Autowired
    TRegisterPayMapper registerPayMapper;
    private static Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);

    @Override
    public OrderInfoBo getOrderInfo(String order_code) {

        return registerPayMapper.getOrderInfo(order_code);
    }

    @Override
    public void updateOrderPrePayStatus(String order_code, Integer pay_way, Integer pay_money, String prepay_id) {
        //新增预支付-下单记录
        Integer order_type = 1;
        Integer pay_status = PayConstants.OrderPayStatus.NOT_PAY;
        registerPayMapper.addPayRecord(order_type,order_code,pay_status,pay_way,pay_money,prepay_id,null);
        //新增订单操作预支付-下单记录

//        registerPayMapper.insertOrderOperateRecord(order_type,order_code,operate_content,operate_person);
    }

    @Override
    @Transactional
    public void updateOrderHasPayStatus(String order_code, Integer pay_way, Integer pay_money, String order_pay_id) {
        //更新订单支付状态
        Integer order_type = 1;
        registerPayMapper.updateConsultOrderPayStatus(order_code,pay_way,pay_money);
        //新增支付记录
        Integer pay_status = PayConstants.OrderPayStatus.HAS_PAY;
        registerPayMapper.insertPayRecord(order_type,order_code,pay_status,pay_way,pay_money,null,order_pay_id);
    }

    @Override
    public void addAgentUserReation(String order_code) {
        if(StrUtil.isEmpty(order_code))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        Integer agent_id = registerPayMapper.getAgentIdByOrderCode(order_code);
        String product_id = registerPayMapper.queryAgentByProduct(order_code);
        AgentUserReationVo vo = new AgentUserReationVo();
        vo.setAgentId(agent_id);
        vo.setProductId(product_id);

        OrderInfoBo orderInfo = registerPayMapper.getOrderInfo(order_code);//5
        if(null != orderInfo && orderInfo.getCount() > 0){
            Integer count_realtion = registerPayMapper.queryRealtionIsExits(agent_id,product_id);//15
            if(null != count_realtion && count_realtion > 0){
                int count  = registerPayMapper.queryCount(agent_id,product_id);
                logger.info("只更新数量");
                //只更新数量
                int current_count = orderInfo.getCount();
                registerPayMapper.updateReationCount(agent_id,product_id,current_count + count);
            }else{
                logger.info("新增----");
                vo.setCount(orderInfo.getCount());
                registerPayMapper.insertAgentUserReation(vo);
            }
        }
    }

    @Override
    public String createOrderCode(AliPrePayReqVo aliPrePayReqVo) {
        String orderCode = null;
        if(aliPrePayReqVo.getCount() != 0){
            String yearMonthDay = SequenceUtils.getSequence();
            StringBuffer sb = new StringBuffer ();
            sb.append("chird").append(yearMonthDay);
            orderCode = sb.toString();
            aliPrePayReqVo.setOrderCode(orderCode);
            registerPayMapper.createOrderCode(aliPrePayReqVo);
            return orderCode;
        }
        return null;
    }


}
